package com.le.tester.leetcode;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 描述：字符串数组的最长公共前缀
 */
public class LongestCommonPrefix {

    private static final Logger LOG = LoggerFactory.getLogger(LongestCommonPrefix.class);

    //没有获取字符串中的某一个字符，只能切三个比较，切出substring as result;
    public String longestCommonPrefix(String[] strs) {
        if (null == strs || 0 == strs.length) {
            return "";
        }
        String tempArr = strs[0];
        int j;
        int strsLen = strs.length;
        for (int i = 1; i < strsLen; i++) {
            for (j = 0; j < strs[i].length() && j < tempArr.length(); j++) {
                if (tempArr.charAt(j) != strs[i].charAt(j)) {
                    break;
                }
            }
            tempArr = tempArr.substring(0, j);
        }
        return tempArr;
    }

    public static void main(String[] args) {
        String[] arr0String = {"dog", "racecar", "car"};
        String[] arr1String = {"flower", "flow", "flight"};
        LongestCommonPrefix longestCommonPrefix = new LongestCommonPrefix();
        String s = longestCommonPrefix.longestCommonPrefix(arr1String);
        LOG.info("result is {}", s);

    }

}
